package com.jk.hly.aspect;


import com.jk.hly.bean.RegisterLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Aspect
@Component
public class RegisterAspect {
    @Resource
    MongoTemplate mongoTemplate;

   @Pointcut("execution(* com.jk.hly.controller.HlyRegisterController.userRegister(..))")
    public void pointCut(){
    }
    @AfterReturning(value = "pointCut()")
    public void AfterLogin(JoinPoint call){

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        String code= (String) request.getSession().getAttribute("code");
        String phone= (String) request.getSession().getAttribute("phone");
        String email= (String) request.getSession().getAttribute("email");
        RegisterLog registerLog=new RegisterLog(phone,email,new Date(),"hello!!欢迎注册！验证码为："+code);
        mongoTemplate.save(registerLog);
    }
}
